package cmdbControllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"lazyops_go/models"
)

type IdcController struct {
	beego.Controller
}

func (c *IdcController) IndexIdc() {
	data_list :=models.QueryIdcAll()
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["label"] = "IDC管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "cmdb/idcjs.html"
	c.TplName = "cmdb/idc.html"
}

func (c *IdcController) SearchIdc() {
	key := c.Ctx.Input.Param(":key")
	data_list :=models.QueryIdcSearch(key)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	c.Data["key"] = key
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["data_list"] = data_list
	c.Data["label"] = "管理用户"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "cmdb/idcjs.html"
	c.TplName = "cmdb/idc.html"
}

func (c *IdcController) CreateIdc() {
	idc_name := c.GetString("idc_name")
	mg_user := c.GetString("mg_user")
	email := c.GetString("email")
	mobile := c.GetString("mobile")
	obj := models.AssetIdc{}
	obj.IdcName = idc_name
	obj.MgUser = mg_user
	obj.Email = email
	obj.Mobile = mobile
	err :=models.AddIdc(obj)
	if err != nil{
		c.Data["json"] = err
	}else{
		c.Data["json"] = "添加成功"
	}
	c.ServeJSON()
}

func (c *IdcController)  GetIdIdc() {
	id,_ := c.GetInt("id",0)
	data := models.QueryIdcOne(id)
	c.Data["json"] = data
	c.ServeJSON()
}

func (c *IdcController) EditIdc()  {
	id,_ := c.GetInt("id",0)
	idc_name := c.GetString("idc_name")
	mg_user := c.GetString("mg_user")
	email := c.GetString("email")
	mobile := c.GetString("mobile")
	obj := models.AssetIdc{}
	obj.IdcName = idc_name
	obj.MgUser = mg_user
	obj.Email = email
	obj.Mobile = mobile
	err := models.UpdateIdc(id,obj)

	if err != nil{
		fmt.Println(id,err)
		c.Data["json"] = err
	}else{
		c.Data["json"] = "更新成功"
	}
	c.ServeJSON()
}

func (c *IdcController) DelIdc() {
	id,_ := c.GetInt("id",0)
	err := models.DeleteIdc(id)
	if err != nil {
		c.Data["json"] = err
	}else{
		c.Data["json"] = "已删除"
	}

	c.ServeJSON()
}

